odoo.define('report_docx.ws_download', function () {
    return function ws_download(data, filename, mimetype) {
        $().ready(function () {
            if (!window.WebSocket) {
                alert("童鞋, 你的浏览器不支持该功能啊");
            }

        });

        function log(text) {
            console.log(text)
        }

        var ws = new WebSocket("ws://127.0.0.1:9001");
        ws.onerror = function (ev) {
            alert("打印客户端链接失败，请启动客户端重新尝试打印。")
        };

        ws.onopen = function (ev) {
            log("成功连接到ws服务器");
            ws.send(filename);

            var reader = new FileReader();
            reader.readAsArrayBuffer(data);
            reader.onload = function (ev) {
                var binaryString = ev.target.result;
                log("开始发送文件"+filename);
                ws.send(binaryString);
                log("文件发送完成"+filename);
            };
        };

        ws.onmessage = function (ev) {
            log("服务器发来消息:"+ev.data)
        };

        ws.close = function (code) {
            log("关闭连接"+code)
        }
    };
});
